#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/13 16:13
# @Author  : 王凯
# @File    : shandong_illegal.py
# @Project : spider-man
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode, parse_url_params


class ShandongIllegalSpider(BaseTaxIllegalSpider):
    name = "shandong_illegal"
    province: str = "山东"
    url: str = "https://etax.shandong.chinatax.gov.cn/dzswj/zdsswfsxajgbl.jsp"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.shandong.chinatax.gov.cn:8443"})
        url = "https://etax.shandong.chinatax.gov.cn/DZSWJ/DZSWJ_SSWFSXAJ_CX_NAVIGATE"
        taxpayer_ids = []
        for tax_id in taxpayer_ids:
            data = {
                "nsrmc": "",
                "nsrsbh": tax_id,
                "zcdz": "",
                "zzjgdm": "",
                "fddbrxm": "",
                "fddbrsfzhm": "",
                "cwfzrxm": "",
                "cwfzrsfzhm": "",
            }
            params = {
                "method": "query",
                "nsrmc": "",
                "nsrsbh": tax_id,
                "zcdz": "",
                "zzjgdm": "",
                "fddbrxm": "",
                "fddbrsfzhm": "",
                "cwfzrxm": "",
                "cwfzrsfzhm": "",
                "cxdq": "",
            }
            yield self.FormRequest(url + "?" + urlencode(params), formdata=data, method="POST", callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        root_url, _ = parse_url_params(response.request.url)
        total_record = response.xpath(".").re_first(r'"count":(\d+)')
        if total_record and int(total_record) >= 1:
            yield from self.parse_detail(response, **kwargs)
            for page in range(2, int(total_record) // 10 + 2):
                params = {
                    "method": "queryMxFh",
                    "nsrmc": "",
                    "nsrsbh": "",
                    "zcdz": "",
                    "zzjgdm": "",
                    "fddbrxm": "",
                    "fddbrsfzhm": "",
                    "cwfzrxm": "",
                    "cwfzrsfzhm": "",
                    "cxnd": "",
                    "cxdq": "",
                    "ajxz": "",
                    "page": page,
                }
                yield self.FormRequest(root_url + "?" + urlencode(params), method="POST", callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        root_url, _ = parse_url_params(response.request.url)
        for i in response.xpath("//tr[not(@class)]"):
            item = NetTaxIllegalProItem()
            params = {
                "method": "queryMx",
                "nsrmc": "",
                "nsrsbh": i.xpath(".//input/@onclick").re_first(r"getXxxx\(\'(.*?)\s*\'\)"),
                "zcdz": "",
                "zzjgdm": "",
                "fddbrxm": "",
                "fddbrsfzhm": "",
                "cwfzrxm": "",
                "cwfzrsfzhm": "",
                "cxnd": "",
                "cxdq": "",
                "ajxz": "",
            }
            data = i.xpath(".//td/text()").getall()
            if len(data) > 3:
                item.company_name = data[1]
                item.taxpayer_id = data[2]
                item.illegal_status = data[3]
            yield self.FormRequest(root_url + "?" + urlencode(params), method="POST", callback=self.parse_detail_info, cb_kwargs={"item": item})

    def parse_detail_info(self, response, **kwargs):
        item = kwargs.get('item')
        item.province = self.province
        mapping = {
            "nsrmc": "company_name",
            "nsrsbh": "taxpayer_id",
            "zzjgdm": "org_code",
            "zcdz": "address",
            "ajxz": "illegal_status",
            "zywfss": "illegal_facts",
            "xgflyjjswclcfqk": "basis_and_punishment",
        }

        for k, v in mapping.items():
            if response.xpath(f"//{k}/text()").get():
                setattr(item, mapping.get(k), response.xpath(f"//{k}/text()").get())
        if response.xpath("//{}/text()".format("fddbrhfzrxm")).get():
            item.legal_representative = "法定代表人或者负责人姓名：{}；性别：{}；证件号码：{}".format(
                response.xpath("//{}/text()".format("fddbrhfzrxm")).get(),
                response.xpath("//{}/text()".format("fddbrhfzrxb")).get(),
                response.xpath("//{}/text()".format("fddbrhfzrzjhm")).get(),
            )
            item.illegal_legal_representative = "违法期间法人代表或者负责人姓名：{}；性别：{}；证件号码：{}".format(
                response.xpath("//{}/text()".format("fddbrhfzrxm")).get(),
                response.xpath("//{}/text()".format("fddbrhfzrxb")).get(),
                response.xpath("//{}/text()".format("fdrzjhm")).get(),
            )
        item.resp_intermediary = response.xpath("//{}/text()".format("fyzjzrdzjjgxx")).get()
        if response.xpath("//{}/text()".format("fyzjzrdcwfzrxm")).get():
            item.resp_financial = "负有直接责任的财务人员姓名：{}；性别：{}；证件号码：{}".format(
                response.xpath("//{}/text()".format("fyzjzrdcwfzrxm")).get(),
                response.xpath("//{}/text()".format("fyzjzrdcwfzrxb")).get(),
                response.xpath("//{}/text()".format("fyzzjhm")).get(),
            )
        year = re.findall(r"((?:20|19)\d{2})[-.年 /]", item.illegal_facts)
        if year:
            item.year = year[-1] or ""
        item.year = item.year or ""
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "shandong_illegal"])
